package com.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.domain.Role;
import com.hc.domain.User;
import com.hc.mapper.PermissionMapper;
import com.hc.mapper.RoleMapper;
import com.hc.mapper.UserMapper;
import com.hc.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.security.Permission;
import java.util.List;

/**
 * 用户
 *
 * @author 梁云亮
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    //@Resource
    //private RelationService relationService;
    //
    //@Resource
    //private BlogService blogService;
    //
    //@Resource
    //private UserBlogAgreeService userBlogAgreeService;
    //

    @Override
    public User getUserByUsername(String username) {
        User user = userMapper.selectOne(new QueryWrapper<User>().select("id","nickname","password","avatar").eq("username", username));
        return user;
    }

    @Override
    public String getUserAuthority(Integer userId) {
        final StringBuffer authority =  new StringBuffer();

        roleMapper.selectRoleByUserId(userId).forEach(role -> {
            authority.append("ROLE_" + role.getCode()).append(",");
            permissionMapper.selectPermissionByRoleId(role.getId()).forEach(permission -> {
                authority.append(permission.getCode()).append(",");
            });
        });
        return authority.deleteCharAt(authority.length()-1).toString(); //删除最后一个,
    }

    @Override
    public List<Integer> getUserIdsByRoleId(Integer roleId) {
        return userMapper.selectUserIdsByRoleId(roleId);
    }

    @Override
    public List<Integer> getUserIdsByPermissionId(Integer PermissionId) {
        return userMapper.selectUserIdsByPermissionId(PermissionId);
    }

    //@Override
    //public int getCountByUsername(String username) {
    //    Integer count = userMapper.selectCount(new QueryWrapper<User>().eq("username", username));
    //    return count;
    //}
    //
    //@Override
    //public int modifyUserInfo(UserVO userVO) {
    //    User user = userMapper.selectById(userVO.getId());
    //    if (user == null) {
    //        throw new GlobalException(404, "要更新的User不存在");
    //    }
    //    BeanUtils.copyProperties(userVO, user);
    //    int res = userMapper.updateById(user);
    //    return res;
    //}
    //
    //@Override
    //public String modifyUserAvatar(MultipartFile avatar) {
    //    return null;
    //}
    //
    //@Override
    //public int modifyUserStatus(Integer userId, Integer status) {
    //    User user = User.builder()
    //            .id(userId)
    //            .status(status)
    //            .build();
    //    int res = userMapper.updateById(user);
    //    return res;
    //}
    //
    //@Override
    //public int modifyUserRole(UserRoleVO userRoleVO) {
    //    return 1;
    //}
    //
    //@Override
    //public PageBean<UserOnlineDTO> listOnlineUsers(UserVO userVO) {
    //
    //    return null;
    //}
    //
    //@Override
    //public int removeOnlineUser(Integer userId) {
    //    return 1;
    //}
}
